package com.sdtv.basemodule.widget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.LogUtils;
import com.sdtv.basemodule.R;
import com.sdtv.basemodule.config.AppConfig;

public class ContentViewWithToolBar extends RelativeLayout {

    //上下文环境
    private Context mContext;
    //
    private LayoutInflater mInflater;
    // 内容布局文件ID
    private int contentLayoutId;

    //页头布局根文件
    private RelativeLayout toolBarLayout;
    // 内容布局区域
    private RelativeLayout contentView;
    //真正显示的toolbar
    private View toolBar;

    /**
     * @param context         上下文环境
     * @param contentLayoutId 内容布局ID（该布局文件不包含toolbar）
     */
    public ContentViewWithToolBar(Context context, int contentLayoutId) {
        super(context);
        this.mContext = context;
        this.mInflater = LayoutInflater.from(mContext);
        this.contentLayoutId = contentLayoutId;
        initView();
    }


    /**
     * 初始化布局文件
     */
    private void initView() {
        View rootView = mInflater.inflate(R.layout.layout_content_view, this);
        toolBarLayout = rootView.findViewById(R.id.tl_toolBarLayout);
        contentView = rootView.findViewById(R.id.tl_contentLayout);
        // 设置页头布局
        initToolBar();
        //设置真正的内容区域
        initContentLayout();
    }

    /**
     * 初始化导航栏
     */
    private void initToolBar() {
        int toolBarId = AppConfig.INSTANCE.getConfig().getToolBarLayoutId();
        if (0 != toolBarId) {
            toolBar = mInflater.inflate(toolBarId, null);
            ViewGroup.LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            toolBarLayout.addView(toolBar, lp);
        }
    }

    /**
     * 设置内容布局
     */
    private void initContentLayout() {
        if (0 != contentLayoutId) {
            View view = mInflater.inflate(contentLayoutId, null);
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            contentView.addView(view, lp);
        } else {
            LogUtils.e("尚未设置布局文件ID");
        }
    }

    public RelativeLayout getContentView() {
        return contentView;
    }

    public void setContentView(RelativeLayout contentView) {
        this.contentView = contentView;
    }

    public View getToolBar() {
        return toolBar;
    }

    public void setToolBar(View toolBar) {
        this.toolBar = toolBar;
    }
}
